package com.google.project.client.ProjectManager;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.DailyReportService;
import com.google.project.client.Service.DailyReportServiceAsync;
import com.google.project.client.Service.LoginService;
import com.google.project.client.Service.LoginServiceAsync;
import com.google.project.client.Service.MessageService;
import com.google.project.client.Service.MessageServiceAsync;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.shared.DailyReportDTO;
import com.google.project.shared.MessageDTO;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.UsersDTO;
import com.google.gwt.user.client.ui.Label;

public class PMHomepage extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField
	Hyperlink hypMessNew;
	@UiField
	Hyperlink hypReportNew;
	@UiField
	Hyperlink hypProjectNew;
	@UiField
	Hyperlink hypProjectFinish;
	@UiField
	Hyperlink hypProjectStart;
	@UiField Label lblTitle;
	@UiField Label lblMess;
	@UiField Label lblDailyReport;
	@UiField Label lblProjInfo;

	private final LoginServiceAsync loginService = GWT
			.create(LoginService.class);
	private final MessageServiceAsync messageService = GWT
			.create(MessageService.class);
	private final DailyReportServiceAsync dailyReportService = GWT
	.create(DailyReportService.class);
	private final ProjectServiceAsync projectService = GWT
	.create(ProjectService.class);
	
	List<MessageDTO> listMessage = new ArrayList<MessageDTO>();
	UsersDTO userDTO = new UsersDTO();
	MessageDTO messDTO = new MessageDTO();

	interface Binder extends UiBinder<Widget, PMHomepage> {
	}

	public PMHomepage() {
	
		initWidget(binder.createAndBindUi(this));
		getNewReportByUserId(); //lấy tất cả report mới
		getAllMess();
		getNumOfProjByStatus(0);
		getNumOfProjByStatus(1);
		getNumOfProjByStatus(2);
	}
	// get info user login from session
	public void getNewReportByUserId() {
		loginService.getCurrentUserDTO(new AsyncCallback<UsersDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(UsersDTO result) {
				userDTO = result;
				getReportByPMID(userDTO.getUserId()); //lấy tất cả report PM chưa đọc
			}
		});
	}
	// get all Message
	public void getAllMess() {
		messageService.getMessageList(new AsyncCallback<List<MessageDTO>>() {

			@Override
			public void onSuccess(List<MessageDTO> result) {
				for (MessageDTO messageDTO : result) {
					listMessage.add(messageDTO);
				}
				LoadMess();
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}

	// get Message where (userId=receiveId and status=1) or (userId=senderId and
	// status=2) & parentID=0
	private void LoadMess() {
		Boolean condition = true;
		List<MessageDTO> temple = new ArrayList<MessageDTO>();

		for (MessageDTO mess : listMessage) {
			condition = condition
					&& (mess.getTypeId() == 1)
					&& (mess.getParentId().equalsIgnoreCase("0") && (((mess
							.getStatus() == 1)
							&& mess.getReceiverId().equals(userDTO.getUserId()) || ((mess
							.getStatus() == 2) && mess.getSenderId().equals(
							userDTO.getUserId())))));
			if (condition) {
				temple.add(mess);
			}
			condition = true;
		}
		//đếm tin nhắn mới
		if (temple != null) {
			hypMessNew.setText("Tin nhắn chưa đọc: " + temple.size() + " (Tin nhắn)");
		}else{
			hypMessNew.setText("Tin nhắn chưa đọc: 0 (tin nhắn)");
		}
	}
	// getAll dailyReport where PMId + statusOfReport=1-chưa đọc
	public void getReportByPMID(String _userID) {
		int status = 1;
		dailyReportService.getDailyReportByPM(_userID, status,
				new AsyncCallback<List<DailyReportDTO>>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("getDailyReportByPM fail!");
					}

					@Override
					public void onSuccess(List<DailyReportDTO> result) {
						if(result.size()==0){
							hypReportNew.setText("Báo cáo chưa đọc: 0 (Báo cáo)");
						}else{
							hypReportNew.setText("Báo cáo chưa đọc: " + result.size() + " (Báo cáo)");
						}
					}
				});
	}
	/***
	 * Lấy danh sách dự án theo userID và trạng thái project
	 * 0-mới, 1-đang tiến hành
	 */
	public static int num=0;
	public void getNumOfProjByStatus(final int status) {
	
		projectService.getProjectList(new AsyncCallback<List<ProjectDTO>>() {

			@Override
			public void onSuccess(List<ProjectDTO> result) {
				List<ProjectDTO> list=new ArrayList<ProjectDTO>();
				for (ProjectDTO projectDTO : result) {
					if(projectDTO.getStatus()==status && projectDTO.getUserId().equals(userDTO.getUserId())){
						list.add(projectDTO);
					}
				}
				num=list.size();
				if(status==0){
					hypProjectNew.setText("Dự án mới: " + num + " (Dự án)");
				}else if(status==1){
					hypProjectStart.setText("Dự án đang triển khai: " + num + " (Dự án)");
				}
				else if(status==2){
					hypProjectFinish.setText("Dự án đã kết thúc: " + num + " (Dự án)");
				}
				lblTitle.setText("THÔNG TIN TỔNG HỢP");
				lblMess.setText("Các vấn đề cần giải quyết :");
				lblDailyReport.setText("Báo cáo hàng ngày :");
				lblProjInfo.setText("Thông tin dự án :");
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Có lỗi trong quá trình Tải thông tin!");
			}
		});
	}

	
	//tin nhắn mới
	@UiHandler("hypMessNew")
	void onHypMessNewClick(ClickEvent event) {
		PMEntrypoint.get().PMMessageList();
	}
	//báo cáo mới
	@UiHandler("hypReportNew")
	void onHypReportNewClick(ClickEvent event) {
		PMEntrypoint.get().PMDailyReportList();
	}

	@UiHandler("hypProjectNew")
	void onHypProjectNewClick(ClickEvent event) {
		int status=0; // dự án mới
		PMEntrypoint.get().PMProjectList(status);
	}

	@UiHandler("hypProjectFinish")
	void onHypProjectFinishClick(ClickEvent event) {
		int status=2; //dự án đã kết thúc
		PMEntrypoint.get().PMProjectList(status);
	}
  
	@UiHandler("hypProjectStart")
	void onHypProjectStartClick(ClickEvent event) {
		int status=1; //dự án đang triển khai
		PMEntrypoint.get().PMProjectList(status);
	}
}
